
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:fyi/common/common_map.dart';
import 'package:fyi/common/constant.dart';
import 'package:fyi/generated/i18n.dart';
import 'package:fyi/net/http_header.dart';
import 'package:fyi/router/route_app.dart';
import 'package:fyi/style/style.dart';
import 'package:fyi/utils/navigator_util.dart';
import 'package:fyi/utils/sp_util.dart';
import 'package:fyi/widget/md_flex_button.dart';

class CommonUtil {

  static void saveToken(String token){
    HttpHeader.setToken(token);
    SPUtil.save(Constant.TOKEN, token);
  }

  static void toast(String msg){
    Fluttertoast.showToast(
        msg: msg,
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIos: 1,
        backgroundColor: Colors.black87,
        textColor: Colors.white,
        fontSize: 16.0
    );
  }

  static void changeLanguage(Locale locale){
    changeLocal(locale);
    HttpHeader.setLanguage(locale.toString());
    saveLanguage(locale);
  }

  static void changeLocaleLanguage(){
    var locale = getSaveLocale();
    if(locale != null){
      changeLocal(locale);
    }
  }

  static void saveLanguage(Locale locale){
    SPUtil.save(Constant.LOCALE_LANGUAGE_CODE, locale.languageCode);
    SPUtil.save(Constant.LOCALE_COUNTRY_CODE, locale.countryCode);
  }

  static S getS(BuildContext context) {
    return S.of(context);
  }

  static getDefaultLocal(){
    Locale current = WidgetsBinding.instance.window.locale;
    var supported = S.delegate.isSupported(current);
    current = supported ? current : S.delegate.supportedLocales[0];
    return current;
  }

  static getSelectedLocale(){
    var locale = getSaveLocale();
    if(locale == null){
      locale = getDefaultLocal();
    }
    return locale;
  }

  static getSaveLocale(){
   var languageCode = CommonMap.instance.get(Constant.LOCALE_LANGUAGE_CODE);
   var countryCode = CommonMap.instance.get(Constant.LOCALE_COUNTRY_CODE);
   if(languageCode != null && languageCode != "" && languageCode.toString() != ""){
     return Locale(languageCode.toString(),countryCode?.toString());
   }
   return null;
  }

  static showLanguageDialog(BuildContext context,Function(Locale) fun) {
    var defaultLocal = getDefaultLocal();
    List<String> list = [
      "default"
    ];
    for (var value in S.delegate.supportedLocales) {
      list.add(value.languageCode);
    }
    CommonUtil.showCommitOptionDialog(context, list, (index) {
      if(index == 0){
        fun(defaultLocal);
      }else{
        fun(S.delegate.supportedLocales[index-1]);
      }
    }, height: 150.0);
  }

  ///列表item dialog
  static Future<Null> showCommitOptionDialog(
      BuildContext context,
      List<String> commitMaps,
      ValueChanged<int> onTap, {
        width = 250.0,
        height = 400.0,
        List<Color> colorList,
      }) {
    return NavigatorUtil.showMDDialog(
        context: context,
        builder: (BuildContext context) {
          return Center(
            child: new Container(
              width: width,
              height: height,
              padding: new EdgeInsets.all(4.0),
              margin: new EdgeInsets.all(20.0),
              decoration: new BoxDecoration(
                color: Color(MDColors.white),
                //用一个BoxDecoration装饰器提供背景图片
                borderRadius: BorderRadius.all(Radius.circular(4.0)),
              ),
              child: new ListView.builder(
                  itemCount: commitMaps.length,
                  itemBuilder: (context, index) {
                    return MDFlexButton(
                      maxLines: 1,
                      mainAxisAlignment: MainAxisAlignment.start,
                      fontSize: 14.0,
                      color: colorList != null
                          ? colorList[index]
                          : Theme.of(context).primaryColor,
                      text: commitMaps[index],
                      textColor: Color(MDColors.white),
                      onPress: () {
                        Navigator.pop(context);
                        onTap(index);
                      },
                    );
                  }),
            ),
          );
        });
  }

  static Future<Null> showAlertDialog(BuildContext context,String title,String content,
      String positiveText,Function() positiveFun,String negativeText,Function() negativeFun){
    return NavigatorUtil.showMDDialog(context:context,builder: (BuildContext context) {
      return new AlertDialog(
        title: new Text(title),
        content: new SingleChildScrollView(
          child: new ListBody(
            children: <Widget>[
              new Text(content),
            ],
          ),
        ),
        actions: <Widget>[
          positiveText != null ? new FlatButton(
            child: new Text(positiveText),
            onPressed: () {
              Navigator.of(context).pop();
              positiveFun();
            },
          ) : null,
          negativeText != null ? new FlatButton(
            child: new Text(negativeText),
            onPressed: () {
              Navigator.of(context).pop();
              negativeFun();
            },
          ) : null,
        ],
      );
    }
    );
  }

}